package com.bitnpulse.dev2.jjh.engstudybook_free.page;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bitnpulse.dev2.jjh.engstudybook_free.R;
import com.bitnpulse.dev2.jjh.engstudybook_free.db.WordDataManager;
import com.bitnpulse.dev2.jjh.engstudybook_free.db.WritingDB;
import com.bitnpulse.dev2.jjh.engstudybook_free.dialog.TwoButtonDialog;
import com.bitnpulse.dev2.jjh.engstudybook_free.listener.IPopUpButtonEventListener;

public class CompositionPage extends Activity{

	private WordDataManager mDataManager_;
	
	public String[] cData = new String[5];
	
	EditText Composition_text;
	
	TextView mWords_text;
	
	String Contents_ID;
	
	private TwoButtonDialog mTwoButtonDialog_;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.compositionpage_layout);
		
		Intent intent = getIntent();
		Contents_ID = intent.getStringExtra("Contents_ID");
		
		mDataManager_ =  new  WordDataManager(getApplicationContext());
		
		Composition_text = (EditText)findViewById(R.id.composition_text);
		Composition_text.setPrivateImeOptions("defaultInputmode=english;");
		
		
		mWords_text = (TextView)findViewById(R.id.words_text);
		
		mTwoButtonDialog_ = new TwoButtonDialog(this);
		
		mTwoButtonDialog_.setButtonListener(new IPopUpButtonEventListener() {
			
			@Override
			public void buttonEvent(int id) {
				switch (id) {
					case 1:
						finish();
						break;
					case 2:
						saveDiary() ;
						finish();
						break;
				}
			}
		});
		
		
		db_Init();
		
		mWords_text.setText(mDataManager_.getWords(Contents_ID, getPackageName()));
	}
	
	protected void db_Init(){
	    	
	    	db_read();
	    }
	
	  public void db_read(){
//			userTable_sqldata = new WritingDB(this);
//	    	userTable_db = userTable_sqldata.getReadableDatabase();
	    	WritingDB mwriting_Db = new WritingDB(this);
	    	
			Cursor c = mwriting_Db.getReadableDatabase().query(WritingDB.TABLE_NAME, WritingDB.FROM, WritingDB.CONTENT_ID + " LIKE " +"'%" + Contents_ID + "%'", null, null, null, null);
			
			
			 while (c.moveToNext()) { 
					cData[0] = c.getString(0);
					cData[1] = c.getString(1);
					cData[2] = c.getString(2);
					cData[3] = c.getString(3);
					cData[4] = c.getString(4);
					
			 }
					
			 if(cData[0] != null && !cData[0].isEmpty()){
				 Composition_text.setText(cData[4]);
				 
			 }
			 

			c.close();
			mwriting_Db.close();
			
		}
	    
		public void btnClick(View view) {
			
			saveDiary() ;
			
			finish();
		}
	    
		
		public void saveDiary() {
			WritingDB mwriting_Db = new WritingDB(this);
			
			ContentValues values = new ContentValues();
			
			
			if(cData[0] == null || cData[0].isEmpty()){
				
				values.put(WritingDB.CONTENT_ID, Contents_ID);
				values.put(WritingDB.DIARY_1, "");
				values.put(WritingDB.DIARY_2, "");
				
				values.put(WritingDB.COMPOSITION, Composition_text.getText().toString());
				
				long table_id = mwriting_Db.getReadableDatabase().insertOrThrow(WritingDB.TABLE_NAME, null, values);
			
			
			}else{
				
				values.put(WritingDB.COMPOSITION, Composition_text.getText().toString());
				
				String sFilter = WritingDB._ID + " LIKE '%" + cData[0] + "%'";
				
				mwriting_Db.getReadableDatabase().update(WritingDB.TABLE_NAME, values, sFilter , null);
			}
	    	mwriting_Db.close();
			
		}
	
		@Override
		public void onBackPressed() {
			
			if(cData[4] != null && cData[4].equals(Composition_text.getText().toString())){
				super.onBackPressed();
			}else if (cData[4] == null && Composition_text.getText().toString().isEmpty()){
				super.onBackPressed();
			}else{
				mTwoButtonDialog_.show();
				mTwoButtonDialog_.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
			}
			
		}
	
}
